package com.dx.as.common;

import android.content.Context;

import com.dx.as.common.bean.DeviceInfo;
import com.dx.as.common.storage.StorageType;
import com.dx.as.common.storage.StorageUtil;
import com.dx.as.common.string.StringUtil;
import com.dx.as.config.Config;
import com.dx.as.config.preference.Preferences;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.dx.as.config.Config.BROAD_ACTION_HOME_DOWN;

/**
 * Created by jwaoo on 2017/5/25.
 */

public class CommonUtils {
    private static boolean enFloat = true;
    private static DeviceInfo deviceInfo;
    private static Map<Integer,TypeToken> typeTokenMap = new HashMap<>();
    public static void addTypeToken(Integer hasCode, TypeToken typeToken){
        typeTokenMap.put(hasCode, typeToken);
    }
    private Map<Integer,TypeToken> getTypeTokenMap(){
        return typeTokenMap;
    }
    public static void removeTypeToken(Integer hasCode){
        typeTokenMap.remove(hasCode);
    }
    public static Type getType(Integer hasCode){
        return typeTokenMap.get(hasCode).getType();
    }
    public static String getDBName(){
        return Config.DB_NAME;
    }
    public static DeviceInfo getDeviceInfo(){
        return deviceInfo;
    }
    public static void setDeviceInfo(DeviceInfo deviceInfo){
        CommonUtils.deviceInfo = deviceInfo;
    }
    public static String getBaseDir(Context context){
        String externalPath = StorageUtil.getBaseDirectory(context);
        return externalPath +"/"+getIMAccount()+"/";
    }

    public static String getLogDir(Context context){
        return getBaseDir(context)+ StorageType.TYPE_LOG.getStoragePath();
    }

    public static String getImgDir(Context context){
        return getBaseDir(context)+StorageType.TYPE_IMAGE.getStoragePath();
    }

    public static String getAudioDir(Context context){
        return getBaseDir(context)+StorageType.TYPE_AUDIO.getStoragePath();
    }

    public static String getVideoDir(Context context){
        return getBaseDir(context)+StorageType.TYPE_VIDEO.getStoragePath();
    }

    public static String getCacheDir(Context context){
        return getBaseDir(context)+StorageType.TYPE_CACHE.getStoragePath();
    }

    public static boolean isLogin(){
        if (StringUtil.isEmpty(getToken())) return false;
        return true;
    }

    public static void saveAccount(String account) {
        Preferences.saveUserAccount(account);
    }

    public static String getAccount(){
        return Preferences.getUserAccount();
    }

    public static void saveIMName(String showName){
        Preferences.saveIMName(showName);
    }

    public static String getIMName(){
        return Preferences.getIMName();
    }

    public static void saveIMAccount(int imAccount){
        Preferences.saveIMAccount(imAccount);
    }

    public static int getIMAccount(){
        return Preferences.getIMAccount();
    }

    public static void saveToken(String token){
        Preferences.saveUserToken(token);
    }

    public static void savePerfect(int perfect){
        Preferences.savePerfect(perfect);
    }

    public static void saveFileUrl(String url) {
        Preferences.saveFileUrl(url);
    }

    public static String getFileUrl() {
       return Preferences.getFileUrl();
    }

    public static void saveRtcUrl(String url) {
        Preferences.saveRtcUrl(url);
    }

    public static String getRtcUrl() {
        return Preferences.getRtcUrl();
    }

    public static void saveChatUrl(String url) {
        Preferences.saveChatUrl(url);
    }

    public static String getChatUrl() {
        return Preferences.getChatUrl();
    }

    public static int getPerfect(){
        return Preferences.getPerfect();
    }

    public static void clearAccountInfo(){
        saveToken("");
        saveAccount("");
        saveIMName("");
        saveIMAccount(0);
        savePerfect(0);
    }
    public static String getToken(){
        return Preferences.getUserToken();
    }
    public final static String getFilePath(String dir,String url){
        if (StringUtil.isEmpty(url)) return null;
        int end = url.lastIndexOf(".");
        if (end == -1){
           return dir + url.substring(url.lastIndexOf("/")+1);
        }
        return dir + url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
    }
    public final static void savePushToken(String token){
        Preferences.savePushToken(token);
    }

    public final static String getPushToken(){
        return Preferences.getPushToken();
    }

    public final static boolean chekEmail(String email){
        String RULE_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        //正则表达式的模式
        Pattern p = Pattern.compile(RULE_EMAIL);
        //正则表达式的匹配器
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public final static String getActionHomeDown(){
        return BROAD_ACTION_HOME_DOWN;
    }

    public static boolean isEnFloat() {
        return enFloat;
    }

    public static void setEnFloat(boolean enFloat) {
        CommonUtils.enFloat = enFloat;
    }
}
